/*
 * JBeagle - a Java toolkit for genetic algorithms.
 * 
 * Copyright (c) 2010 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package jbeagle.core;

import java.io.Serializable;
import java.util.Collection;

/**
 * Provides a flexible way to implement constraints on {@code Individual}s.
 *
 * @author Matthijs Snel
 * @see AbstractIndividual
 */
public class Constraints<E> implements Serializable {

	private static final long serialVersionUID = -3926100715102560050L;

	/**
	 * Checks whether the specified element violates any constraints. Returns false by
	 * default, override if a different behaviour is desired.
	 * 
	 * @param e the element to be added to the {@code Individual}
	 * @return true if the element violates the constraints.
	 */
    protected boolean violates(E e) {
        return false;
    }
    
    /**
	 * Checks whether the specified collection violates any constraints.
	 * 
	 * @param e the collection to be added to the {@code Individual}
	 * @return the first element found that violates the constraints, or {@code null}
	 * if there is no such element.
	 */
    protected E violates(Collection<? extends E> c) {
        for( E e : c )
            if( violates(e) )
                return e;
        return null;
    }
}
